# -*- coding: utf-8 -*-

'''
    【简介】 用tkinter开发重命名文件小程序

'''
import os
import tkinter as tk
from tkinter import filedialog, ttk, messagebox


class GUI:

    def __init__(self):
        self.root = tk.Tk()
        self.root.title('重命名小程序')
        self.root.geometry("300x200+1100+150")
        self.interface()

    def interface(self):
        """"界面编写位置"""
        # 文件夹
        self.file_label = tk.Label(self.root, text="文件夹：")
        self.file_label.grid(row=0, column=0)
        self.file_dir = tk.Entry(self.root, font=('宋体', 10), fg='black')
        self.file_dir.grid(row=0, column=1)
        self.file_btn = tk.Button(self.root, text="浏览文件", command=self.get_dir, fg='black')
        self.file_btn.grid(row=0, column=2)

        # 模式
        self.mode_label = tk.Label(self.root, text="模　式：")
        self.mode_label.grid(row=1, column=0)

        self.mode_combobox = ttk.Combobox(
            master=self.root,  # 父容器
            # height=10,  # 高度,下拉显示的条目数量
            width=17,  # 宽度
            state='',  # 设置状态 normal(可选可输入)、readonly(只可选)、 disabled(禁止输入选择)
            cursor='arrow',  # 鼠标移动时样式 arrow, circle, cross, plus...
            font=('宋体', 10),  # 字体、字号
            # textvariable=self.mode_val,  # 通过StringVar设置可改变的值
            values=['添加前缀', '替换'],  # 设置下拉框的选项
        )
        self.mode_combobox.bind("<<ComboboxSelected>>", self.pick)
        self.mode_combobox.grid(row=1, column=1)

        # 前缀
        self.pre_label = tk.Label(self.root, text="前缀：")
        # self.pre_label.grid(row=2, column=0)
        self.pre_entry = tk.Entry(self.root, font=('宋体', 10), fg='black')
        # self.pre_entry.grid(row=2, column=1)

        # old
        self.old_label = tk.Label(self.root, text="将　　：")
        # self.old_label.grid(row=2, column=0)
        self.old_entry = tk.Entry(self.root, font=('宋体', 10), fg='black')
        # self.old_entry.grid(row=2, column=1)

        # new
        self.new_label = tk.Label(self.root, text="替换成：")
        # self.new_label.grid(row=3, column=0)
        self.new_entry = tk.Entry(self.root, font=('宋体', 10), fg='black')
        # self.new_entry.grid(row=3, column=1)

        # 提交
        self.commit_btn = tk.Button(self.root, text="提交", command=self.run, fg='blue')
        self.commit_btn.grid(row=5, column=1, columnspan=2)

    def get_dir(self):
        '''获取单个文件'''
        file_path = filedialog.askdirectory()
        self.file_dir.delete(0)
        self.file_dir.insert(0, file_path)

    def pick(self, e):
        mode = self.mode_combobox.get()
        self.del_unit()
        if mode == '添加前缀':
            self.pre_label.grid(row=2, column=0)
            self.pre_entry.grid(row=2, column=1)
        elif mode == '替换':
            self.old_label.grid(row=2, column=0)
            self.old_entry.grid(row=2, column=1)
            self.new_label.grid(row=3, column=0)
            self.new_entry.grid(row=3, column=1)

    def del_unit(self):
        lst = [self.pre_entry, self.pre_label, self.old_label, self.old_entry, self.new_entry, self.new_label]
        for i in lst:
            i.grid_forget()

    def run(self):
        # print(self.entry.get())
        file_dir = self.file_dir.get()
        success = True
        if not file_dir:
            messagebox.showerror("错误", message='请选择需要操作的文件夹')
            success = False
        mode = self.mode_combobox.get()
        if mode == '添加前缀':
            pre = self.pre_entry.get()
            self.add_pre(file_dir, pre)
        elif mode == '替换':
            old = self.old_entry.get()
            new = self.new_entry.get()
            self.rename(file_dir, old, new)
        else:
            messagebox.showerror("错误", message='请选择正确的操作模式')
            success = False
        if success:
            messagebox.showinfo("成功", message='恭喜！执行成功')
            self.root.destroy()

    def rename(self, path, old, new):
        files = os.listdir(path)

        for file in files:
            dir = os.path.join(path, file)
            # 检测是否为文件夹，如果是，则递归
            if os.path.isdir(dir):
                self.rename(dir, old, new)
                continue
            file = file.replace(old, new)
            new_dir = os.path.join(path, file)
            try:
                os.rename(dir, new_dir)
            except:
                pass

    def add_pre(self, path, pre):
        files = os.listdir(path)

        for file in files:
            dir = os.path.join(path, file)
            # 检测是否为文件夹，如果是，则递归
            if os.path.isdir(dir):
                self.add_pre(dir, pre)
                continue
            file = pre + file
            new_dir = os.path.join(path, file)
            os.rename(dir, new_dir)


if __name__ == '__main__':
    gui = GUI()
    gui.root.mainloop()
